//
//  ZPBFistPageCollectionViewCell.m
//  WZLProduct
//
//  Created by boys on 16/3/18.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "ZPBFistPageCollectionViewCell.h"
#import "ZPBHeartCollectionReusableView.h"
#import "ZPBSecondCollectionViewCell.h"
#import "ZPBHeartOneCollectionReusableView.h"
#import "ZPBOneCollectionViewCell.h"
#import "ZPBTwoCollectionViewCell.h"
#import "ZPBThreeCollectionViewCell.h"
#import "ZPBFirstGetModel.h"
#import "ZPBSecondGetModel.h"
#import "ZPBThirdGetModel.h"
#import "RankButtonViewController.h"
#import "ZPBLongListModel.h"



@interface ZPBFistPageCollectionViewCell ()<UICollectionViewDelegateFlowLayout,UICollectionViewDelegate, UICollectionViewDataSource,HTLBDelegate>



@property (nonatomic, strong) UICollectionView *collectionView; ///collectionView上的collectionView
@property (nonatomic, strong) NSMutableArray *dataSource; ///数据源
@property (nonatomic, strong) NSMutableArray *ButtonDataSource;
@property (nonatomic, retain) NSMutableArray *arrButtonModel;

/**
 下49 导航64
 */
@end

@implementation ZPBFistPageCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.dataSource = [ NSMutableArray array];
        self.ButtonDataSource = [NSMutableArray array];
        [self createCollectionView];
        [self getData];

        
        
    }
    return self;
}

#pragma mark - 创建collectionView
- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.itemSize = CGSizeMake(0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 35, kScreenWidth, kScreenHeight - 149) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    

    [self.contentView addSubview:_collectionView];
    
    //注册头
    [_collectionView registerClass:[ZPBHeartCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [_collectionView registerClass:[ZPBHeartOneCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZPBHeartOneCollectionReusableView"];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionReusableView"];
    
    [_collectionView registerClass:[ZPBSecondCollectionViewCell class ]forCellWithReuseIdentifier:@"ZPBSecondCollectionViewCell"];
    [_collectionView registerClass:[ZPBOneCollectionViewCell class] forCellWithReuseIdentifier:@"ZPBOneCollectionViewCell"];
    [_collectionView registerClass:[ZPBTwoCollectionViewCell class] forCellWithReuseIdentifier:@"ZPBTwoCollectionViewCell"];
    [_collectionView registerClass:[ZPBSecondCollectionViewCell class] forCellWithReuseIdentifier:@"ZPBSecondCollectionViewCell"];
    [_collectionView registerClass:[ZPBThreeCollectionViewCell class] forCellWithReuseIdentifier:@"ZPBThreeCollectionViewCell"];
}


//给头视图赋值
- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    ZPBFirstGetModel *firstModel = _dataSource[indexPath.section];
    NSArray *arr = firstModel.data;
    
    
    if (kind == UICollectionElementKindSectionHeader) {
        if (indexPath.section == 0) {
            ZPBHeartCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
            header.lbView.htLBdelegate = self;
            
            NSMutableArray *picArray = [NSMutableArray array];
            for (ZPBSecondGetModel *model in arr) {
                [picArray addObject:model.picUrl];
            }
            //gcd 方法只走一次
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                if (picArray.count) {
                    header.lbView.sendScrollImageDataArray(picArray);
                }
            });
            
            return header;
            
            
        }else{
            
            ZPBHeartOneCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"ZPBHeartOneCollectionReusableView" forIndexPath:indexPath];
            
            ZPBFirstGetModel *model = _dataSource[indexPath.section];
            header.model = model;
            
            return header;
            
        }
    }
    if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionReusableView" forIndexPath:indexPath];
        
        footer.backgroundColor = [UIColor blackColor];
        footer.alpha = 0.1;
        return footer;
    }
    return nil;
    
}

//轮播代理跳转
- (void)LBView:(HTLBView *)lbView DidTapTishImageAtIndex:(NSUInteger)index{
//    NSLog(@"%ld",index);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(kScreenWidth, 1);
}

//头视图的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        
        return CGSizeMake(kScreenWidth, kScreenHeight / 4);
    }else if (section == 1){
        return CGSizeZero;
    }
    return CGSizeMake(kScreenWidth, 40);
}

//每个分区的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeZero;
    }else if(indexPath.section == 1 ){
        return CGSizeMake([UIScreen mainScreen].bounds.size.width / 5, 90);
        
    }else if (indexPath.section == 2 || indexPath.section == 5  || indexPath.section == 8 || indexPath.section == 9){
        return CGSizeMake([UIScreen mainScreen].bounds.size.width , 80);
        
        
    }else if (indexPath.section == 3  || indexPath.section == 4 || indexPath.section == 6 ||indexPath.section == 7) {
        return CGSizeMake((kScreenWidth - 40) / 3 , 150);
        
    }
    return CGSizeMake(0, 0);
}
//cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }if (section == 1) {
        return 4;
    }if (section == 2) {
        return 3;
    }if (section == 3) {
        return 6;
    }if (section == 4) {
        return 3;
    }if (section == 5) {
        return 3;
    }if (section == 6) {
        return 3;
    }if (section == 7) {
        return 3;
    }
    if (section == 8) {
        return 3;
    }
    if (section == 9) {
        return 1;
    }
    
    return 1;
}
//section的数量
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return _dataSource.count;
}
//设置 layout大小
- (UIEdgeInsets )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 10, 10, 10);
}
//cell赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZPBFirstGetModel *firstModel = _dataSource[indexPath.section];
    NSArray *arr = firstModel.data;
    ZPBSecondGetModel *thirdModel = arr[0];
    NSArray *arr2 = thirdModel.songs;
    
    if (indexPath.section == 0) {
        ZPBSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZPBSecondCollectionViewCell" forIndexPath:indexPath];
        return cell;
    }else if(indexPath.section == 1 ) {
        ZPBSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZPBSecondCollectionViewCell" forIndexPath:indexPath];
        ZPBSecondGetModel *model = arr[indexPath.row];
       

        cell.model =  model;
        return cell;
        
    }else if(indexPath.section == 2){
        ZPBOneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZPBOneCollectionViewCell" forIndexPath:indexPath];
        ZPBThirdGetModel *model = arr2[indexPath.row];
         NSArray *arr = @[@"1", @"2", @"3"];
        cell.model = model;
        cell.numberLabel.text = arr[indexPath.row];

        return cell;
        
    }else if(indexPath.section == 3) {
        ZPBTwoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZPBTwoCollectionViewCell" forIndexPath:indexPath];
        ZPBSecondGetModel *model = arr[indexPath.row];
        cell.model = model;

        return cell;
        
    }else if(indexPath.section == 4) {
        ZPBTwoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZPBTwoCollectionViewCell" forIndexPath:indexPath];
        ZPBSecondGetModel *model = arr[indexPath.row];
        cell.model = model;
        return cell;
        
    }else if(indexPath.section ==5) {
        ZPBThreeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZPBThreeCollectionViewCell" forIndexPath:indexPath];
        ZPBSecondGetModel *model = arr[indexPath.row];
        cell.model = model;
        return cell;
        
    }else if(indexPath.section == 6) {
        ZPBTwoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZPBTwoCollectionViewCell" forIndexPath:indexPath];
        ZPBSecondGetModel *model = arr[indexPath.row];
        cell.model = model;
        return cell;
        
    }else if(indexPath.section == 7){
        ZPBTwoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZPBTwoCollectionViewCell" forIndexPath:indexPath];
        ZPBSecondGetModel *model = arr[indexPath.row];
        cell.model = model;
        return cell;
        
    }else if(indexPath.section == 8){
        ZPBThreeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZPBThreeCollectionViewCell" forIndexPath:indexPath];
        ZPBSecondGetModel *model = arr[indexPath.row];
        cell.model = model;
        return cell;
        
    }else if (indexPath.section == 9){
        ZPBThreeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZPBThreeCollectionViewCell" forIndexPath:indexPath];
        ZPBSecondGetModel *model = arr[indexPath.row];
        cell.model = model;
        return cell;
        
    }
    
    return nil;
}
#pragma mark - colleciton点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0 && indexPath.section == 1) {

        
        [self.delegate selectCell];
    
        
    }else if (indexPath.row == 1 && indexPath.section == 1){
        
        [self.delegate selectCell2];
    }else if (indexPath.row == 2 && indexPath.section == 1){
        [self.delegate selectCell3];
    }
}

- (void)getData{
    [RequestData GETRequestWithURLString:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1458261090988&app=ttpod&v=v8.1.5.2016022418&user_id=0&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=9.2&active=0&net=2&openudid=b86e6033a473164bf5304fe8cb61411d62e74884&idfa=0E349B7A-F8C4-4CB9-88DA-E6EE45A230A9&utdid=VutKrJQl3sEDAHWnWnZbS%2B%2FE&alf=201200&bundle_id=com.ttpod.music&latitude=38.88263088967935&longtitude=121.5393002610967" completionWithSuccess:^(id result) {
        
        
        [self setData:result];
        
    } WithFailure:^(id error) {
        [self setData:error];
    }];
}






- (void)setData:(id)dataBoge{
    NSArray *arr = dataBoge[@"data"];
    for (NSDictionary *dic in arr) {
        ZPBFirstGetModel *model = [[ZPBFirstGetModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.dataSource addObject:model];
    }
    
    NSLog(@"%@", self.dataSource);
    [self.collectionView reloadData];
}



@end
